package com.liwei.vip.mall.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liwei.mall.util.RespResult;
import com.liwei.vip.mall.goods.model.Brand;
import com.liwei.vip.mall.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /***
     * 添加
     */
    @PostMapping
    public RespResult add(@RequestBody Brand brand) {
        brandService.save(brand);
        return RespResult.ok();
    }

    /**
     * 删
     */
    @DeleteMapping("/{id}")
    public RespResult delete(@PathVariable("id") String id) {
        brandService.removeById(id);
        return RespResult.ok();
    }

    /**
     * 改
     */
    @PutMapping
    public RespResult update(@RequestBody Brand brand) {
        brandService.updateById(brand);
        return RespResult.ok();
    }

    /**
     * 查
     */
    @GetMapping("/{id}")
    public RespResult findById(@PathVariable("id") String id) {
        brandService.getById(id);
        return RespResult.ok();
    }

    /**
     * 条件查询
     */
    @GetMapping("/search")
    public RespResult<List<Brand>> queryList(@RequestBody Brand brand) {
        List<Brand> brands = brandService.queryList(brand);
        return RespResult.ok(brands);
    }

    /**
     * 条件分页查询
     */
    @GetMapping("/search/{page}/{size}")
    public RespResult<Page<Brand>> queryPageList(@PathVariable("page") Long page,
                                                     @PathVariable("size") Long size,
                                                     @RequestBody Brand brand) {
        Page<Brand> brandsInfo = brandService.queryPageList(brand,page,size);
        return RespResult.ok(brandsInfo);
    }

    /**
     * 根据分类id查询品牌集合
     */
    @GetMapping("/category/{pid}")
    public RespResult<List<Brand>> categoryBrands(@PathVariable(value="pid") Integer pid){
        List<Brand> brands = brandService.queryByCategoryId(pid);
        return RespResult.ok(brands);
    }
}
